package tasoloikka;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Main {
  
  public static BufferedImage bImg;
  private int screenWidth = 1024;
  private int screenHeight = 640;
  private Canvas canvas;
  private Game game;
  private ActionListener actionListener;
  
  public Main() {
    
    actionListener = new ActionListener();
    game = new Game(actionListener,screenWidth,screenHeight);
    canvas = new Canvas();
    
    actionListener.setGame(game);
    
    JFrame frame = new JFrame();
    
    canvas.addKeyListener(actionListener);
    canvas.addMouseListener(actionListener);
    canvas.addMouseMotionListener(actionListener);
    
    canvas.setPreferredSize(new Dimension(screenWidth,screenHeight));
    
    frame.add(canvas);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.setFocusable(false);
    frame.setLocation(400, 300);
    canvas.requestFocus();
  }

  public static void main(String[] args) {
   Main main = new Main();
   main.loop();
  }
  
  public void loop(){
    bImg = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
    Graphics gbi = bImg.createGraphics();
    
    while(true){
      try {

        //Lasketaan muutokset
        game.update();
        
        //Haetaan piirrettävä materiaali
        game.paint(gbi);
        
        //Piirretään uudestaa
        canvas.repaint();
        
        Thread.sleep(33);
      } catch (Exception ex){
        ex.printStackTrace();
        break;
      }
    }
    
  }
}